﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Imaging;
using System.Dynamic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.Text.RegularExpressions;
using System.Xml;
using ImageProcessor;
using ImageProcessor.Common.Extensions;
using ImageProcessor.Imaging.Formats;
using WeiTap.Article.Dal;
using WeiTap.Article.Model;
using WeiTap.SearchEngin.Sogou;
using WeiTap.Web;
using ZYB.Security;

namespace DaZhong.Test
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("密钥：");
           
            var strTemp = Encode();
            Console.WriteLine("消息打包完成");
            var document=new XmlDocument();
            document.LoadXml(strTemp);
            DecodeBody(document);
            Console.WriteLine("消息解包完成");
            Console.ReadLine();
        }

        public static string Encode()
        {
            var document=new XmlDocument();
            var msg = document.CreateElement("MSG");
            document.AppendChild(msg);
            var head = document.CreateElement("HEAD");
            msg.AppendChild(head);
            var code = document.CreateElement("CODE");
            code.InnerText = "103";
            head.AppendChild(code);
            var sid = document.CreateElement("SID");
            sid.InnerText = "00001";
            head.AppendChild(sid);
            var timestamp = document.CreateElement("TIMESTAMP");
            timestamp.InnerText = DateTime.Now.Ticks.ToString();
            head.AppendChild(timestamp);
            var serviceid = document.CreateElement("SERVICEID");
            serviceid.InnerText = "01010101";
            head.AppendChild(serviceid);
            var body = document.CreateElement("BODY");
            body.InnerText = GetEncodeBody();
            msg.AppendChild(body);
            Console.WriteLine("打包好的消息:");
            Console.WriteLine(document.OuterXml);
            return document.OuterXml;
        }

        public static string GetEncodeBody()
        {
            var document =new XmlDocument();

            var body = document.CreateElement("BODY");
            document.AppendChild(body);
            var section =document.CreateElement("SECTION");
            var name = document.CreateElement("NAME");
            name.InnerText = "CORPNAME";
            var value = document.CreateElement("VALUE");
            value.InnerText = "华为技术有限公司";
            section.AppendChild(name);
            section.AppendChild(value);
            body.AppendChild(section);
             section = document.CreateElement("SECTION");
             name = document.CreateElement("NAME");
            name.InnerText = "CORPTELE";
             value = document.CreateElement("VALUE");
            value.InnerText = "28780808";
            section.AppendChild(name);
            section.AppendChild(value);
            body.AppendChild(section);
            Console.WriteLine("组织好的消息体XML文档：");
            Console.WriteLine(document.OuterXml);

            return Security.Encrypt(document.OuterXml);
        }

        public static string DecodeBody(XmlDocument doc)
        {
            var strbody = doc.SelectSingleNode("MSG/BODY")?.InnerText;
            return Security.Decrypt(strbody);
        }


    }
   
}
